/*     */ package com.bj58.analysis.tools.common;
/*     */ 
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.net.URLDecoder;
/*     */ import java.net.URLEncoder;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ import org.apache.commons.lang.StringUtils;
/*     */ 
/*     */ public class URLDecodeUtils
/*     */ {
/*  16 */   private static final String[] PS = { "`", "~", "!", "@", "#", "$", "^", "&", "(", ")", ":", ";", "\"", "'", "<", ",", ">", "?", "/", "=", "{", "}", "[", "]" };
/*     */   
/*     */ 
/*     */ 
/*  20 */   private static String[] psEncode = null;
/*     */   
/*     */   private static final String PATTER_GBK_UTF8 = "%[0-9a-fA-F]{2}";
/*     */   
/*     */   private static final String PATTER_UNICODE = "%u[0-9a-fA-F]{4}";
/*     */   
/*     */   private static final String PATTER_E = "%[^0-9a-fA-F]{2}";
/*     */   private static final String PATTER_P = "[^=+\\s\\p{Punct}A-Za-z0-9一-龥ࠀ-一\\x3130-\\x318F\\xAC00-\\xD7A3，。！￥（）《》？、”“·‘’；：【】]+";
/*     */   
/*     */   static
/*     */   {
/*  31 */     psEncode = new String[PS.length];
/*  32 */     for (int i = 0; i < PS.length; i++) {
/*     */       try {
/*  34 */         String code = URLEncoder.encode(PS[i], "utf-8");
/*  35 */         psEncode[i] = code;
/*     */       } catch (UnsupportedEncodingException e) {
/*  37 */         e.printStackTrace();
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*  43 */   private static Pattern partternOfGbk_utf8 = getPattern("%[0-9a-fA-F]{2}");
/*  44 */   private static Pattern partternOfUnicode = getPattern("%u[0-9a-fA-F]{4}");
/*  45 */   private static Pattern partternOfP = getPattern("[^=+\\s\\p{Punct}A-Za-z0-9一-龥ࠀ-一\\x3130-\\x318F\\xAC00-\\xD7A3，。！￥（）《》？、”“·‘’；：【】]+");
/*  46 */   private static Pattern partternOfE = getPattern("%[^0-9a-fA-F]{2}");
/*     */   
/*     */   private static String parserUrl(String url) throws Exception
/*     */   {
/*  50 */     url = url.replace("\\x", "%");
/*  51 */     url = replaceOfP(url);
/*  52 */     url = replaceOfUP(url);
/*  53 */     url = url.replaceAll("%20", " ");
/*  54 */     Matcher m_a_unicode = partternOfUnicode.matcher(url);
/*  55 */     boolean unicodeFlag = false;
/*  56 */     while (m_a_unicode.find()) {
/*  57 */       unicodeFlag = true;
/*  58 */       String tmp = m_a_unicode.group();
/*  59 */       String code1 = "%" + tmp.substring(2, 4);
/*  60 */       String code2 = "%" + tmp.substring(4, 6);
/*  61 */       url = url.replaceAll(tmp, code1 + code2);
/*     */     }
/*     */     
/*  64 */     if (unicodeFlag) {
/*  65 */       url = exchangeUp(url);
/*  66 */       String tmpUrl = URLDecoder.decode(url, "unicode");
/*  67 */       return tmpUrl;
/*     */     }
/*     */     
/*  70 */     Matcher m_a_gbk_utf8 = partternOfGbk_utf8.matcher(url);
/*  71 */     int number = 0;
/*  72 */     while (m_a_gbk_utf8.find()) {
/*  73 */       number++;
/*     */     }
/*     */     
/*  76 */     url = url.replace("%%", "%25%");
/*  77 */     if ((number % 3 == 0) && (number % 2 != 0)) {
/*  78 */       String tmpUrl = parserGbk_Utf8(url, "utf-8", "gbk");
/*  79 */       return tmpUrl; }
/*  80 */     if ((number % 3 != 0) && (number % 2 == 0)) {
/*  81 */       String tmpUrl = parserGbk_Utf8(url, "gbk", "utf-8");
/*  82 */       return tmpUrl; }
/*  83 */     if ((number % 3 == 0) && (number % 2 == 0)) {
/*  84 */       String tmpUrl = parserGbk_Utf8(url, "utf-8", "gbk");
/*  85 */       return tmpUrl;
/*     */     }
/*  87 */     String tmpUrl = parserGbk_Utf8(url, "utf-8", "gbk");
/*  88 */     return tmpUrl;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private static String parserGbk_Utf8(String url, String codeSrc, String newCode)
/*     */     throws Exception
/*     */   {
/*  97 */     url = exchangeUp(url);
/*  98 */     String tmpUrl = URLDecoder.decode(url, codeSrc);
/*  99 */     Matcher m_a_p = partternOfP.matcher(tmpUrl);
/* 100 */     boolean tmpFlag = m_a_p.find();
/* 101 */     if (tmpFlag) {
/* 102 */       tmpUrl = URLDecoder.decode(url, newCode);
/*     */     }
/* 104 */     return tmpUrl;
/*     */   }
/*     */   
/*     */   private static Pattern getPattern(String pattern)
/*     */   {
/* 109 */     return Pattern.compile(pattern, 2);
/*     */   }
/*     */   
/*     */   private static String exchangeUp(String url)
/*     */   {
/* 114 */     List<String> list = null;
/* 115 */     Matcher m_e = partternOfE.matcher(url);
/* 116 */     while (m_e.find()) {
/* 117 */       if (list == null)
/* 118 */         list = new ArrayList();
/* 119 */       String value = m_e.group();
/* 120 */       list.add(value);
/*     */     }
/* 122 */     if ((list != null) && (list.size() != 0)) {
/* 123 */       for (String tmpStr : list) {
/* 124 */         String tmpCode = tmpStr.replace("%", "%25");
/* 125 */         url = url.replace(tmpStr, tmpCode);
/*     */       }
/*     */     }
/*     */     
/* 129 */     return url;
/*     */   }
/*     */   
/*     */   private static String replaceOfUP(String url)
/*     */   {
/* 134 */     for (int i = 0; i < psEncode.length; i++) {
/* 135 */       String code = psEncode[i];
/* 136 */       String ps = PS[i];
/* 137 */       if (code.equals("%24"))
/* 138 */         ps = "\\" + PS[i];
/* 139 */       url = url.replaceAll(code, ps);
/* 140 */       url = url.replaceAll(code.toLowerCase(), ps);
/*     */     }
/* 142 */     return url;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static String replaceOfP(String line)
/*     */   {
/* 151 */     if ((StringUtils.isNotBlank(line)) && 
/* 152 */       (line.contains("%25"))) {
/* 153 */       line = line.replace("%25", "%");
/* 154 */       if (line.contains("%25")) {
/* 155 */         line = replaceOfP(line);
/*     */       }
/*     */     }
/*     */     
/* 159 */     return line;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static String parser(String url)
/*     */   {
/* 169 */     if (StringUtils.isBlank(url)) {
/* 170 */       return null;
/*     */     }
/* 172 */     String tmpStr = null;
/*     */     try {
/* 174 */       tmpStr = parserUrl(url);
/*     */     } catch (Exception e) {
/* 176 */       e.printStackTrace();
/*     */     }
/* 178 */     return tmpStr;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static String getSearchEngines(String url)
/*     */   {
/* 188 */     String newUrl = null;
/* 189 */     if (StringUtils.isNotBlank(url)) {
/* 190 */       String pattern = "https://|http://";
/* 191 */       Pattern p_a = Pattern.compile(pattern, 2);
/* 192 */       Matcher m_a = p_a.matcher(url);
/* 193 */       while (m_a.find()) {
/* 194 */         String a = m_a.group();
/* 195 */         if (StringUtils.isNotBlank(a)) {
/* 196 */           int index = url.indexOf(a);
/* 197 */           String tmpSub = url.substring(index + a.length(), url.length());
/*     */           
/* 199 */           int tmpIndex = tmpSub.indexOf("/");
/* 200 */           if (tmpIndex == -1)
/* 201 */             tmpIndex = tmpSub.length();
/* 202 */           String sec = tmpSub.substring(0, tmpIndex);
/* 203 */           newUrl = a + sec;
/* 204 */           break;
/*     */         }
/*     */       }
/*     */     }
/* 208 */     return newUrl;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static String getDomain(String url)
/*     */   {
/* 218 */     String tmpUrl = null;
/* 219 */     url = getSearchEngines(url);
/* 220 */     if (StringUtils.isNotBlank(url)) {
/* 221 */       String[] urls = url.split("/", -1);
/* 222 */       if (urls.length >= 3)
/* 223 */         tmpUrl = urls[2];
/*     */     }
/* 225 */     return tmpUrl;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static String getWord(String src)
/*     */   {
/* 236 */     String word = null;
/* 237 */     if (StringUtils.isNotBlank(src)) {
/* 238 */       String pattern = "(\\?|&)(wd=|word=|q=|query=|w=|kw=|keyword=|p=)";
/* 239 */       Pattern p_a = Pattern.compile(pattern, 2);
/* 240 */       Matcher m_a = p_a.matcher(src);
/* 241 */       if (m_a.find()) {
/* 242 */         String a = m_a.group();
/* 243 */         if (a.contains("?")) {
/* 244 */           a = "\\" + a;
/*     */         }
/* 246 */         String[] srcs = src.split(a, -1);
/* 247 */         if (srcs.length > 1) {
/* 248 */           int index = srcs[1].indexOf("&");
/* 249 */           int tmpIndex = index == -1 ? srcs[1].length() : index;
/* 250 */           word = srcs[1].substring(0, tmpIndex);
/*     */         } else {
/* 252 */           word = srcs[0];
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 257 */     return word;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static String getUrlOfParam(String url, String paramName)
/*     */   {
/* 269 */     String tmpStr = null;
/* 270 */     if (url.contains("?")) {
/* 271 */       String[] referers = url.split("\\?", -1);
/* 272 */       String param = referers[1].trim();
/* 273 */       if (StringUtils.isNotBlank(param)) {
/* 274 */         Map<String, String> map = parserCommonParams(param);
/* 275 */         if ((map != null) && (map.size() != 0)) {
/* 276 */           tmpStr = (String)map.get(paramName);
/*     */         }
/*     */       }
/*     */     }
/* 280 */     return tmpStr;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Map<String, String> parserCommonParams(String comParams)
/*     */   {
/* 290 */     Map<String, String> map = null;
/* 291 */     if (StringUtils.isNotBlank(comParams)) {
/* 292 */       map = new HashMap();
/* 293 */       String[] params = comParams.split("&", -1);
/* 294 */       for (String tmp : params) {
/* 295 */         if (tmp.contains("=")) {
/* 296 */           String[] tmps = tmp.split("=", 2);
/* 297 */           map.put(tmps[0], tmps[1]);
/*     */         }
/*     */       }
/*     */     }
/* 301 */     return map;
/*     */   }
/*     */ }


/* Location:              C:\Users\zhaoxiang\Desktop\basestat_log_common_tools-0.3.jar!\com\bj58\analysis\tools\common\URLDecodeUtils.class
 * Java compiler version: 5 (49.0)
 * JD-Core Version:       0.7.1
 */